﻿@using OBUCare.Web.Infrastructure;
@using OBUCare.Core.Models;
@using OBUCare.Core.BL
@using OBUCare.Core.DataAccess;
@{  
    ObuDropDownListHlper ddlHelper = new ObuDropDownListHlper();
                    //this.Layout = "~/Views/Shared/_MinLayout.cshtml";
}
<h2 style="text-align: center;">
    元器件型号号段配置</h2>
<div>
    <table style="width: 100%">
        <tr>
            <td style="width: 10%; text-align: left; height: 40px;">
                厂商：
            </td>
            <td style="width: 10%; text-align: left">
                @Html.Telerik().DropDownList().Name("mSupplier").BindTo(ddlHelper.GetSuppliers(UIConstants.AllItems, false, new KeyValuePair<string, string>(UIConstants.AllItemsValue.ToString(), UIConstants.AllItems))).ClientEvents(events => events.OnChange("onSupplierChange"))
            </td>
            <td style="width: 12%; text-align: right">
                型号名称：
            </td>
            <td style="width: 10%; text-align: left">
                @Html.Telerik().DropDownList().Name("mSpecName").HtmlAttributes(new { @style = "width:160px;" }).BindTo(ddlHelper.GetPartsSpecs(null, true, new KeyValuePair<string, string>(UIConstants.Customize, UIConstants.Customize))).ClientEvents(events => events.OnChange("onSpecNameChange"))
            </td>
            <td style="width: 26%; text-align: left">
                <div id="divText">
                    @Html.TextBox("mtxtSpecName", "", new { @style = "width:140px;font-size:16px" })
                    <span style="color: Red">请输入型号名称</span>
                </div>
            </td>
            <td  style="width: 10%; text-align: right;">
                元器件：
            </td>
            <td style="width: 10%; text-align: left;">
                @Html.Telerik().DropDownList().Name("PartType").BindTo(ddlHelper.GetPartTypes("请选择", true, new KeyValuePair<string, string>("请选择","请选择")))
            </td>
            <td style="width: 10%; text-align: left">
                <input id="btnSubmit" type="button" value="提交" onclick="AddSpec(); " />
            </td>
        </tr>
    </table>
</div>
<div>
    @(Html.Telerik().Grid<View_Contract_Details>()
        .Name("mGrid")
        .DataKeys(keys => keys.Add(c => c.ID))
        .Sortable(sorting => sorting.SortMode(GridSortMode.SingleColumn))
        .DataBinding(dataBinding => dataBinding
            .Ajax()
            .Select("ListContracts", "PartsSpec", new { supplier = UIConstants.AllItemsValue })
                    )
        .Columns(columns =>
        {
            columns.Bound(o => o.ID)
                .ClientTemplate("<input name='checkedRecords' type='checkbox'   value='<#= ID #>' title='选中当前行'/> <#= ++index #>")
                .HeaderTemplate
                (
                    @<text>
    <input type="checkbox" title="全选" id="checkAllRecords" />
    </text>
                ).Width("10%");
            columns.Bound(o => o.START_NO).Width("15%");
            columns.Bound(o => o.END_NO).Width("15%");
            columns.Bound(o => o.SPEC_NAME).Width("10%");
            columns.Bound(o => o.SOFT_VERSION).Width("8%");
            columns.Bound(o => o.HARD_VERSION).Width("8%");
            columns.Bound(o => o.KEY_VERSION).Width("8%");
        }).TableHtmlAttributes(new { @style = "width:100%" })
                .ClientEvents(events =>
                {
                    events.OnError("onError");
                    events.OnDataBinding("onDataBinding");
                })
                .Pageable(paging =>
                {
                    paging.Style(CommonStyleForUI.Grid_PageStyle)
                    .Position(CommonStyleForUI.Grid_PagePosition)
                    .PageTo(CommonStyleForUI.Grid_CurrentPage)
                    .PageSize(CommonStyleForUI.Grid_PageSize);
                })
            //.Resizable(resizing => resizing.Columns(true))
                .Scrollable(scroll => scroll.Height(CommonStyleForUI.Grid_Height))
            //.Selectable()
        )
</div>
<script language="javascript" type="text/javascript">

    $(document).ready(function () {

        //enable或disable自定义型号名
        onSpecNameChange(null);

        //为全选框注册事件
        $('#checkAllRecords').click(function () {
            $("#mGrid tbody input:checkbox").attr("checked", this.checked);
        });


        //$('#btnSubmit').click(function () { AddSpec(); });
    });

    //型号名称下拉列表变动事件
    function onSpecNameChange(e) {
        var dropDownList = $("#mSpecName").data("tDropDownList");
        var partTypeDDL = $("#PartType").data("tDropDownList");
        var value = dropDownList.value();
        if (value == '@UIConstants.Customize') {
            //            $("#mtxtSpecName").removeAttr("disabled");
            $("#divText").removeAttr("style", "display:inline;");
            partTypeDDL.value("请选择");
        }
        else {
            $("#mtxtSpecName").val("");
            //            $("#mtxtSpecName").attr("disabled", "disabled");
            $("#divText").attr("style", "display:none;");
            var jdata = {
                specName: $("#mSpecName").val()
            };
            //当型号名称不为自定义时则获取对应的元器件名称
            $.post("/admin/PartsSpec/GetPartsTypeBySpec", jdata,
                    function (data) {
                        if (data.success) {
                            if (partTypeDDL != null) {
                                partTypeDDL.value(data.partsType);
                            }
                        }
                        else {
                            alert("添加失败:" + data.message)
                        }
                    });

        }
    }

    function onSupplierChange(e) {
        SupplierChanged(onSpecNameChange);
    }

    function SupplierChanged(callback) {

        var grid = $('#mGrid').data('tGrid')
        grid.rebind();

        $("#mGrid input:checkbox").attr("checked", false);

        //厂商编号
        var supplier = $("#mSupplier").data("tDropDownList").value();

        var d = new Date();
        var dateTimeNow = d.getTime();

        var bindDataTypeUrl = '@Url.Action("GetDDLPartsSpecs", "PartsSpec")' + "/" + supplier + "?dt=" + dateTimeNow;
        BindDropDownList("", bindDataTypeUrl, 'mSpecName', callback);
    }


    //添加型号
    function AddSpec() {

        //获取被选中的ID        
        strIDs = getCheckedIDs();
        var specName = $("#mtxtSpecName").val().Trim();
        var specID = null;

        var supplierDDL = $("#mSupplier").data("tDropDownList");
        //下拉列表的厂商编号
        var supplierID = supplierDDL.value();

        //元器件下拉
        var partsTypeDDl = $("#PartType").data("tDropDownList");
        var PartType = partsTypeDDl.value();

        if (strIDs.StringIsNullOrEmpty() && (supplierID == '@UIConstants.AllItemsValue.ToString()')) {
            alert("请选择厂商");
            return false;
        }
        if ($("#PartType").val().Trim() == "请选择") {
            alert("请选择元器件");
            return false;
        }

        if (strIDs.StringIsNullOrEmpty() && specName.StringIsNullOrEmpty()) {
            alert("请选择至少一个合同信息");
            return false;
        }

        if (specName.StringIsNullOrEmpty()) {
            var dropDownList = $("#mSpecName").data("tDropDownList");
            specID = dropDownList.value();
        }
        if (specID == '@UIConstants.Customize' && specName == "") {
            alert("请输入型号名称");
            return false;
        }
        var jdata = {
            strIDs: strIDs,
            supplier: supplierID,
            specID: specID,
            specName: specName,
            PartType: PartType
        };
        $.post("/admin/PartsSpec/ConfirmAddSpec", jdata,
                    function (data) {

                        if (data.success) {
                            alert("添加成功");

                            SupplierChanged(onSpecNameChange);

                            var grid = $('#mGrid').data('tGrid')
                            grid.rebind();

                            var grid = $('#MasterGrid').data('tGrid')
                            grid.rebind();
                        }
                        else {
                            alert("添加失败:" + data.message)
                        }
                    });

    }

    //获取被选中的ID
    function getCheckedIDs() {
        //获取被选中的ID
        var strBuff = new StringBuffer();
        $("#mGrid tbody input:checkbox[name =checkedRecords][checked]").each(function () {
            strBuff.append(this.value);
        });
        strIDs = strBuff.arrayToString(",");
        return strIDs;
    }

    function onError(e) {
        //debugger;
        e.preventDefault();
        /*if (e.XMLHttpRequest.status == "505")
        alert("系统字段不允许删除");
        else if (e.XMLHttpRequest.status == "506")
        alert("系统表不允许删除");
        else*/
        alert(e.XMLHttpRequest.getResponseHeader("message"));
    }

    var index = 0;

    function onDataBinding(e) {
        //debugger;
        var s = $("#mSupplier").data("tDropDownList").value();

        var grid = $('#mGrid').data('tGrid')
        index = (grid.currentPage - 1) * grid.pageSize;
        e.data = {
            // the key ("category") specifies the variable name of the action method which will contain the specified value
            supplier: s
        };
    }


    //绑定Dropdownlist
    function BindDropDownList(value, url, ctrID, callback) {

        var ddlCtr = $('#' + ctrID).data('tDropDownList');
        ddlCtr.loader.showBusy();
        $.get(url, function (data) {
            ddlCtr.dataBind(data);
            ddlCtr.loader.hideBusy();
            if (!value.StringIsNullOrEmpty())
                ddlCtr.value(value);
            else
                ddlCtr.select(0);

            if (callback != null)
                callback(value);
        });
    }
</script>
@{
    ddlHelper.Dispose();    
}
